<script setup>
import {ref} from 'vue'
import {userLoginService, userRegisterService} from '@/api/user.js'
import {User, Lock} from '@element-plus/icons-vue'
import {useUserStore} from '@/stores/index.js'
import {useRouter} from 'vue-router'


const userStore = useUserStore();
const router = useRouter();
const isLogin = ref(true);
const form = ref();
const formModule = ref({
  username: '',
  password: '',
  repeatPassword: '',
});

const loginBtn = async () =>{
  await form.value.validate();
  const res = await userLoginService(formModule.value);
  userStore.setToken(res.data.token);
  ElMessage.success("登录成功");
  await router.push('/home');
}

const registerBtn = async () =>{
  console.log("开始注册");
  await form.value.validate();
  const res = await userRegisterService(formModule.value);
  ElMessage.success("注册成功");
  clearForm();
  isLogin.value = true;
}

const clearForm = () => {
  formModule.value.username = '';
  formModule.value.password = '';
  formModule.value.repeatPassword = '';
}

const rules = {
  username: [{required: true, message: '请输入用户名', trigger: 'blur'},
             {min: 4, max:20, message: '用户名长度需要在4-20位之中', trigger: 'blur'}],

  password: [{required: true, message: '请输入密码', trigger: 'blur'},
             {pattern: /^\S{4,20}$/, message: '密码需要在4-20位之中的非空字符', trigger: 'blur'}],
  repeatPassword: [{validator: (rule, value, callback) => {
      if (value === formModule.value.password) {
        callback();
      }
      else {
        callback(new Error('两次输入密码不一致'));
      }
    }, trigger: 'blur'}],
}
</script>

<template>
  <el-row class="centered-form">
    <el-col :span="4">
      <el-form v-if="isLogin" :model="formModule" :rules="rules" ref="form" label-width="auto" autocomplete="off">
        <el-form-item>
          <h2>登录</h2>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="formModule.username" placeholder="请输入用户名" :prefix-icon="User"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formModule.password" type="password" placeholder="请输入密码"
                    :prefix-icon="Lock" @keyup.enter.native="loginBtn"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginBtn">登录</el-button>
          <el-button @click="isLogin=false">注册</el-button>
        </el-form-item>
      </el-form>

      <el-form v-else :model="formModule" :rules="rules" ref="form" label-width="auto">
        <el-form-item>
          <h2>注册</h2>
        </el-form-item>
        <el-form-item prop="username">
          <!--suppress JSValidateTypes -->
          <el-input v-model="formModule.username" :prefix-icon="User" placeholder="注册的用户名"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formModule.password" type="password" placeholder="请输入密码"
                    :prefix-icon="Lock"/>
        </el-form-item>
        <el-form-item prop="repeatPassword">
          <el-input v-model="formModule.repeatPassword" type="password" placeholder="再次输入密码"
                    :prefix-icon="Lock" @keyup.enter.native="registerBtn"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="registerBtn">注册</el-button>
          <el-button @click="isLogin=true">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.centered-form {
  height: 100vh; /* 设置高度为视口高度 */
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>